package cn.itcast.web.controller.cargo;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ExportDao;
import cn.itcast.dao.cargo.ExportProductDao;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.PackingService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@RequestMapping("/cargo/packing")
@Controller
public class PackingController extends BaseController {
    @Reference
    private PackingService packingService;
    @Autowired
    private ExportDao exportDao;
    @Autowired
    private ExportProductDao exportProductDao;
    @Autowired
    private ContractDao contractDao;

    @RequestMapping("/toExport")
    public String toExport(String id) {
        //判断
        PackingList packingList = new PackingList();
        //合同号
        String exportNos = "";
        //总净重
        double netWeights = 0;
        //毛重
        double grossWeights = 0;
        //总体积
        double totalVolume = 0;
        //装箱费用
        double packingMoney = 0;
        String[] split = id.split(",");
        if (split != null && split.length > 0) {
            for (int i = 0; i < split.length; i++) {
                String exportId = split[i];
                //根据报运单id查询报运单对象
                Export export = exportDao.selectByPrimaryKey(exportId);
                //计算合同号
                exportNos += export.getCustomerContract();
                //通过id查询报运单商品
                ExportProductExample exportProductExample = new ExportProductExample();
                exportProductExample.createCriteria().andExportIdEqualTo(exportId);
                List<ExportProduct> exportProducts = exportProductDao.selectByExample(exportProductExample);

                if (exportProducts != null && exportProducts.size() > 0) {
                    //遍历报运单商品
                    for (ExportProduct exportProduct : exportProducts) {
                        //计算总重量
                        if (exportProduct.getNetWeight() != null) {
                            netWeights += exportProduct.getNetWeight();
                        } else {
                            netWeights += 0;
                        }
                        //计算总毛重
                        if (exportProduct.getGrossWeight() != null) {
                            grossWeights += exportProduct.getGrossWeight();
                        } else {
                            grossWeights += 0;
                        }
                        //总体积
                        if (exportProduct.getSizeLength() != null && exportProduct.getSizeHeight() != null && exportProduct.getGrossWeight() != null) {
                            totalVolume += exportProduct.getSizeLength() * exportProduct.getGrossWeight() * exportProduct.getSizeHeight();
                        } else {
                            totalVolume += 0;
                        }
                    }
                }
                //装箱费用
                packingMoney = grossWeights * 520;
            }
            //封装装箱单对象
            packingList.setExportNos(exportNos);
            packingList.setNetWeights(netWeights);
            packingList.setGrossWeights(grossWeights);
            packingList.setTotalVolume(totalVolume);
            packingList.setPackingMoney(packingMoney);
        }
        request.setAttribute("packingList", packingList);
        request.setAttribute("id", id);
        return "/cargo/pack/packing-toPacking";
    }

    /**
     * 显示装箱列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize) {
        PageInfo<PackingList> pageInfo = packingService.findAll(pageNum, pageSize);
        request.setAttribute("pageInfo", pageInfo);
        return "/cargo/pack/packing-list";
    }

    /**
     * 保存装箱单到装箱列表
     *
     * @param packingList 装箱单对象
     * @param contractIds 报运单ids
     * @return
     */
    @RequestMapping("/toEdit")
    public String toEdit(PackingList packingList, String contractIds) {
        //对装箱时间的判断
        //获取装箱时间的时间毫秒值
        long pt = packingList.getPackingTime().getTime();
        long nowTime = new Date().getTime();
        if(pt<nowTime){
            request.setAttribute("msg","装箱时间必须大于当前时间");
            request.setAttribute("id",contractIds);
            return "/cargo/pack/packing-toPacking";
        }
        String[] split = contractIds.split(",");
        //获取报运单id
        String exportId=split[0];
        //获取报运单对象
        Export export = exportDao.selectByPrimaryKey(exportId);
        String conIds = export.getContractIds();
        String[] splitConIds = conIds.split(",");
        String splitConId = splitConIds[0];
        Contract contract = contractDao.selectByPrimaryKey(splitConId);
        packingList.setBuyer(contract.getOfferor());
        packingList.setSeller(contract.getCompanyName());
        packingList.setCompanyId(getLoginCompanyId());
        packingList.setCompanyName(getLoginCompanyName());
        packingList.setCreateTime(new Date());
        packingList.setCreateDept(getLoginUser().getDeptName());

        packingService.save(packingList, contractIds);
        return "redirect:/cargo/export/list";
    }

    @RequestMapping("/delete")
    public String delete(String id) {
        packingService.delete(id);
        return "redirect:/cargo/packing/list";
    }

    @RequestMapping("/findById")
    @ResponseBody
    public long findStateById(String id) {
        long state = packingService.findStateById(id);
        return state;
    }

    /**
     * 检查生成装箱单按钮时报运单的状态
     * @param id
     * @return
     */
    @RequestMapping("/checkInfo")
    @ResponseBody
    public Integer checkInfo(String id) {
        String oldInfo = null;
        String[] split = id.split(",");
        if (split != null && split.length > 0) {
            for (int i = 0; i < split.length; i++) {
                String exportId = split[i];
                //根据报运单id查询报运单对象
                Export export = exportDao.selectByPrimaryKey(exportId);
                //先判断报运单状态
                if(export.getState()==0){//等于0表示草稿状态
                    return 3;
                }else if(export.getState()==1){
                    return 4;
                } else if(export.getState()==4){//等于4表示已装箱状态
                    return 2;
                }
                String newInfo = export.getShipmentPort() + export.getDestinationPort() + export.getConsignee();
                //id数量为1,不作比较
            if (i == 0) {
                oldInfo = newInfo;
            } else if (oldInfo.equals(newInfo)) {
                oldInfo = newInfo;
            } else {
                return 0;
            }
        }
        }
        return 1;
    }



}
